package com.lxp.project.system.service.impl;

import com.lxp.project.system.domain.SysUserRole;
import com.lxp.project.system.mapper.SysUserRoleMapper;
import com.lxp.project.system.service.ISysUserRoleService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {
    @Override
    public int deleteUserRoleByUserId(Long userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysUserRole::getUserId, userId);
        return this.mapper.deleteByQuery(wrapper);
    }

    @Override
    public int deleteUserRole(Long[] ids) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in(SysUserRole::getUserId, Arrays.asList(ids));
        return this.mapper.deleteByQuery(wrapper);
    }

    @Override
    public long countUserRoleByRoleId(Long roleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysUserRole::getRoleId, roleId);
        return this.count(wrapper);
    }

    @Override
    @Transactional
    public int batchUserRole(List<SysUserRole> userRoleList) {
        return this.saveBatch(userRoleList) ? userRoleList.size() : 0;
    }

    @Override
    public int deleteUserRoleInfo(SysUserRole userRole) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysUserRole::getUserId, userRole.getUserId());
        wrapper.eq(SysUserRole::getRoleId, userRole.getRoleId());
        return this.mapper.deleteByQuery(wrapper);
    }

    @Override
    public int deleteUserRoleInfos(Long roleId, Long[] userIds) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysUserRole::getRoleId, roleId);
        wrapper.in(SysUserRole::getUserId, Arrays.asList(userIds));
        return this.mapper.deleteByQuery(wrapper);
    }
}
